//
//  WZC_WebViewController.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/30.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit
import WebKit

class WZC_WebViewController: WZC_ViewController {
    
    var urlString:String?
    
    lazy var progress: UIProgressView = {
        
        let pro = UIProgressView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 40))
        pro.backgroundColor = .white
        pro.tintColor = UIColor.hexColor(hexadecimal: "#865B2E")
        view.addSubview(pro)
        return pro
    }()
    
    lazy var webView: WKWebView = {
        
        let config = WKWebViewConfiguration()
        
        let web : WKWebView = WKWebView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH), configuration: config)
        web.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
        web.addObserver(self, forKeyPath: "title", options: .new, context: nil)
        
        return web
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        layoutUI()
        let url:URL = URL(string: urlString ?? "www.baidu.com")!
        let urlRequest:URLRequest = URLRequest(url: url)
        self.webView.load(urlRequest)
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        if keyPath == "estimatedProgress" {
            
            self.progress.progress = Float(self.webView.estimatedProgress)
            if self.progress.progress == 1{
                
                self.progress.isHidden = true
            }
            
        }
        else if keyPath == "title"  {
            
            title = self.webView.title
        }
        else
        {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }
    
    deinit {
        
        self.webView.removeObserver(self, forKeyPath: "estimatedProgress", context: nil)
        self.webView.removeObserver(self, forKeyPath: "title", context: nil)
    }
    
}

extension WZC_WebViewController:WKNavigationDelegate
{
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        
        self.progress.isHidden = false
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        self.title = self.webView.title
    }
}

extension WZC_WebViewController
{
    func layoutUI() -> Void {
        
        
        self.view.addSubview(self.webView)
        self.webView.snp.makeConstraints { (make) in
            
            make.edges.equalToSuperview()
        }
        
        self.webView.navigationDelegate = self
        
        self.progress.snp.makeConstraints { (make) in
            
            make.left.right.equalToSuperview()
            make.top.equalTo(0)
            make.height.equalTo(2)
        }
    }
}
